/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nct.model;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author annn
 */
public class DataProvider {

    private static DataProvider Instance = new DataProvider();

    public static DataProvider getInstance() {
        if (Instance != null) {
            return Instance;
        } else {
            return Instance = new DataProvider();
        }
    }
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "nct_tttt";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "tieuhao";

    String strCon = "jdbc:mysql://localhost:3306/nct_tttt?user=root&password=tieuhao&useUnicode=true&characterEncoding=utf8";
    //String charsetforUnicode = 
    Connection conn = null;
    Statement st = null;
    PreparedStatement pst = null;

    public DataProvider() {
        try {
            try {
                Class.forName(driver).newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     Open connection
     */
    public void Open() throws SQLException {
        //jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=utf8"
        //conn = DriverManager.getConnection(url + dbName, userName, password);
        
        conn = DriverManager.getConnection(strCon);
        st = conn.createStatement();
    }

    /*
     Open with paramater connection
     */
    public PreparedStatement OpenWithParamater(String query) throws SQLException {
//        
//         String url = "jdbc:mysql://localhost:3306/";
//    String dbName = "nct_tttt";
//    String driver = "com.mysql.jdbc.Driver";
//    String userName = "root";
//    String password = "tieuhao";

        //conn = DriverManager.getConnection(url + dbName + userName + password);
        conn = DriverManager.getConnection(strCon);
        pst = conn.prepareStatement(query);
        return pst;
    }

    /*
     Select
     */
    public ResultSet ExecuteReader(String query) {
        try {
            return st.executeQuery(query);
        } catch (SQLException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /*
     Select
     */
    public ResultSet ExecuteReaderPreparedStatement() {
        try {
            return pst.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    /*
     Insert
     */
    public int ExecuteNonQuerytoIncrease(String query) {
        try {
            return st.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    /*
     Insert, Update, Delete
     */
    public int ExecuteNonQuery(String query) {
        try {
            return st.executeUpdate(query);
        } catch (SQLException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    /*
     Insert, Update, Delete
     */

    public int ExecuteNonQueryPreparedStatement() {
        try {
            return pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    /*
     Close connection
     */
    public void Close() throws SQLException {
        conn.close();
    }
}
